package com.itheima.tliaswebmanagement.controller;

import com.itheima.tliaswebmanagement.pojo.common.PageResult;
import com.itheima.tliaswebmanagement.pojo.common.Result;
import com.itheima.tliaswebmanagement.pojo.dto.EmpDTO;
import com.itheima.tliaswebmanagement.pojo.dto.EmpPageQueryDTO;
import com.itheima.tliaswebmanagement.pojo.entity.Emp;
import com.itheima.tliaswebmanagement.pojo.vo.EmpVO;
import com.itheima.tliaswebmanagement.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description EmpController
 * @Author ziguan ling
 * @Date 2025--01--06
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    /**
     * 查询分页员工列表
     */
    @GetMapping
    public Result page(EmpPageQueryDTO empPageQueryDTO){
        //1.接收请求参数：kv格式，实体类/多个变量/map
        //选择定义实体类接收多个kv参数：属性名与参数名一致
        log.info("查询分页员工列表：{}",empPageQueryDTO);

        //2.调用service
        PageResult<EmpVO> pageResult = empService.page(empPageQueryDTO);

        //3.封装返回结果
        return Result.success(pageResult);//code=1,msg="success",data=total+rows
    }

    @PostMapping
    public Result add(@RequestBody EmpDTO empDTO){
        log.info("添加员工:{}",empDTO);

        empService.addEmp(empDTO);

        return Result.success();
    }

    @GetMapping("/{id}")
    public Result queryById(@PathVariable Integer id){

        log.info("根据id查询: {}", id);

        EmpVO empVO = empService.queryById_s(id);

        return Result.success(empVO);
    }


    @GetMapping("/list")
    public Result queryAll(){
        List<Emp>  list = empService.queryAll_s();
        return Result.success(list);
    }


    @PutMapping
    public Result update(@RequestBody EmpDTO empDTO){

        empService.update_s(empDTO);

        return Result.success();
    }



    @DeleteMapping
    public Result remove(@RequestParam List<Integer> ids){
        log.info("删除员工: {}", ids);
        empService.removeBatch_s(ids);
        return Result.success();
    }










































}















